<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EOS.IO: contracts/eoslib/singleton.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="eos-logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">EOS.IO
   &#160;<span id="projectnumber">DAWN3.0.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_ae9b15dd87e066e1908bbd90e8f38627.html">contracts</a></li><li class="navelem"><a class="el" href="dir_3fea0bf1ce879ce73e6ecec98007efcd.html">eoslib</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">singleton.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="singleton_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#pragma once</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="db_8hpp.html">eoslib/db.hpp</a>&gt;</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="raw_8hpp.html">eoslib/raw.hpp</a>&gt;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="keyword">namespace  </span><a class="code" href="namespaceeosio.html">eosio</a> {</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;   <span class="keyword">template</span>&lt;account_name Code, u<span class="keywordtype">int</span>64_t SingletonName, <span class="keyword">typename</span> T&gt;</div><div class="line"><a name="l00015"></a><span class="lineno"><a class="line" href="classeosio_1_1singleton.html">   15</a></span>&#160;   <span class="keyword">class </span><a class="code" href="classeosio_1_1singleton.html">singleton</a></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;   {</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;      <span class="keyword">public</span>:</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;         <span class="comment">//static const uint64_t singleton_table_name = N(singleton);</span></div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;</div><div class="line"><a name="l00020"></a><span class="lineno"><a class="line" href="classeosio_1_1singleton.html#a0c3a5437e9b68a71009537a53a9bfbc5">   20</a></span>&#160;         <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classeosio_1_1singleton.html#a0c3a5437e9b68a71009537a53a9bfbc5">exists</a>( <a class="code" href="group__types.html#ga38500bd43c27c05b7f8a566cefe94656">scope_name</a> scope = Code ) {</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;            <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> key = SingletonName;</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;            <span class="keyword">auto</span> read = <a class="code" href="group__dbi64.html#gad3fd3e46fff8f036d13405ffea3694ef">load_i64</a>( scope, Code, key, (<span class="keywordtype">char</span>*)&amp;key, <span class="keyword">sizeof</span>(key) );</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;            <span class="keywordflow">return</span> read &gt; 0;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;         }</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div><div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="classeosio_1_1singleton.html#ad4e16f663b7232ef1ba4f17c29639943">   26</a></span>&#160;         <span class="keyword">static</span> T <span class="keyword">get</span>( <a class="code" href="group__types.html#ga38500bd43c27c05b7f8a566cefe94656">scope_name</a> scope = Code ) {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;            <span class="keywordtype">char</span> temp[1024+8];</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;            *<span class="keyword">reinterpret_cast&lt;</span><a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> *<span class="keyword">&gt;</span>(temp) = SingletonName;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;            <span class="keyword">auto</span> read = <a class="code" href="group__dbi64.html#gad3fd3e46fff8f036d13405ffea3694ef">load_i64</a>( scope, Code, SingletonName, temp, <span class="keyword">sizeof</span>(temp) );</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;            <a class="code" href="group__systemcapi.html#gae2a6ebaf48fe7c69f847ae18f109d09a">assert</a>( read &gt; 0, <span class="stringliteral">&quot;singleton does not exist&quot;</span> );</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;            <a class="code" href="classeosio_1_1datastream.html">datastream&lt;const char*&gt;</a> ds(temp + <span class="keyword">sizeof</span>(SingletonName), read);</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;            T result;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;            <a class="code" href="namespaceeosio_1_1raw.html#ab1d3be0baf9bbbf0977000e24a3bec92">raw::unpack</a>( ds, result );</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;            <span class="keywordflow">return</span> result;</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;         }</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;</div><div class="line"><a name="l00038"></a><span class="lineno"><a class="line" href="classeosio_1_1singleton.html#ac8837aa55f7093b6dc6c3caf991d74ca">   38</a></span>&#160;         <span class="keyword">static</span> T <a class="code" href="classeosio_1_1singleton.html#ac8837aa55f7093b6dc6c3caf991d74ca">get_or_create</a>( <a class="code" href="group__types.html#ga38500bd43c27c05b7f8a566cefe94656">scope_name</a> scope = Code, <span class="keyword">const</span> T&amp; def = T() ) {</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;            <span class="keywordtype">char</span> temp[1024+8];</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;            *<span class="keyword">reinterpret_cast&lt;</span><a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> *<span class="keyword">&gt;</span>(temp) = SingletonName;</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;            <span class="keyword">auto</span> read = <a class="code" href="group__dbi64.html#gad3fd3e46fff8f036d13405ffea3694ef">load_i64</a>( scope, Code, SingletonName, temp, <span class="keyword">sizeof</span>(temp) );</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;            <span class="keywordflow">if</span>( read &lt; 0 ) {</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;               <span class="keyword">set</span>( def, scope );</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;               <span class="keywordflow">return</span> def;</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;            }</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            <a class="code" href="classeosio_1_1datastream.html">datastream&lt;const char*&gt;</a> ds(temp + <span class="keyword">sizeof</span>(SingletonName), read);</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;            T result;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;            ds &gt;&gt; result;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;            <span class="keywordflow">return</span> result;</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;         }</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;</div><div class="line"><a name="l00055"></a><span class="lineno"><a class="line" href="classeosio_1_1singleton.html#a897f32b63fe7330d130d3702cae66cc8">   55</a></span>&#160;         <span class="keyword">static</span> <span class="keywordtype">void</span> <span class="keyword">set</span>( <span class="keyword">const</span> T&amp; value = T(), <a class="code" href="group__types.html#ga38500bd43c27c05b7f8a566cefe94656">scope_name</a> scope = Code ) {</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;            <span class="keyword">auto</span> size = <a class="code" href="namespaceeosio_1_1raw.html#ade628b6c0aa3341fa50e77bbf96cf256">raw::pack_size</a>( value );</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;            <span class="keywordtype">char</span> buf[size+ <span class="keyword">sizeof</span>(SingletonName)];</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;            <a class="code" href="group__systemcapi.html#gae2a6ebaf48fe7c69f847ae18f109d09a">assert</a>( <span class="keyword">sizeof</span>(buf) &lt;= 1024 + 8, <span class="stringliteral">&quot;singleton too big to store&quot;</span> );</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;            <a class="code" href="classeosio_1_1datastream.html">datastream&lt;char*&gt;</a> ds( buf, size + <span class="keyword">sizeof</span>(SingletonName) );</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            ds &lt;&lt; SingletonName;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            ds &lt;&lt; value;</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;            </div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;            <a class="code" href="group__dbi64.html#gab722acdec925f6ac9c7a8a38f42c78d6">store_i64</a>( scope, SingletonName, buf, <span class="keyword">sizeof</span>(buf) );</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;         }</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;   };</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;} </div><div class="ttc" id="namespaceeosio_html"><div class="ttname"><a href="namespaceeosio.html">eosio</a></div><div class="ttdoc">Used to forward declare raw functions. </div><div class="ttdef"><b>Definition:</b> account.hpp:12</div></div>
<div class="ttc" id="group__dbi64_html_gad3fd3e46fff8f036d13405ffea3694ef"><div class="ttname"><a href="group__dbi64.html#gad3fd3e46fff8f036d13405ffea3694ef">load_i64</a></div><div class="ttdeci">int32_t load_i64(account_name scope, account_name code, table_name table, void *data, uint32_t datalen)</div></div>
<div class="ttc" id="group__systemcapi_html_gae2a6ebaf48fe7c69f847ae18f109d09a"><div class="ttname"><a href="group__systemcapi.html#gae2a6ebaf48fe7c69f847ae18f109d09a">assert</a></div><div class="ttdeci">void assert(uint32_t test, const char *cstr)</div><div class="ttdoc">Aborts processing of this action and unwinds all pending changes. </div></div>
<div class="ttc" id="raw_8hpp_html"><div class="ttname"><a href="raw_8hpp.html">raw.hpp</a></div></div>
<div class="ttc" id="namespaceeosio_1_1raw_html_ade628b6c0aa3341fa50e77bbf96cf256"><div class="ttname"><a href="namespaceeosio_1_1raw.html#ade628b6c0aa3341fa50e77bbf96cf256">eosio::raw::pack_size</a></div><div class="ttdeci">size_t pack_size(const T &amp;v)</div><div class="ttdef"><b>Definition:</b> raw.hpp:333</div></div>
<div class="ttc" id="group__types_html_gaaa5d1cd013383c889537491c3cfd9aad"><div class="ttname"><a href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a></div><div class="ttdeci">unsigned long long uint64_t</div><div class="ttdef"><b>Definition:</b> types.h:18</div></div>
<div class="ttc" id="classeosio_1_1singleton_html_ac8837aa55f7093b6dc6c3caf991d74ca"><div class="ttname"><a href="classeosio_1_1singleton.html#ac8837aa55f7093b6dc6c3caf991d74ca">eosio::singleton::get_or_create</a></div><div class="ttdeci">static T get_or_create(scope_name scope=Code, const T &amp;def=T())</div><div class="ttdef"><b>Definition:</b> singleton.hpp:38</div></div>
<div class="ttc" id="classeosio_1_1singleton_html"><div class="ttname"><a href="classeosio_1_1singleton.html">eosio::singleton</a></div><div class="ttdef"><b>Definition:</b> singleton.hpp:15</div></div>
<div class="ttc" id="group__dbi64_html_gab722acdec925f6ac9c7a8a38f42c78d6"><div class="ttname"><a href="group__dbi64.html#gab722acdec925f6ac9c7a8a38f42c78d6">store_i64</a></div><div class="ttdeci">int32_t store_i64(account_name scope, table_name table, const void *data, uint32_t datalen)</div></div>
<div class="ttc" id="group__types_html_ga38500bd43c27c05b7f8a566cefe94656"><div class="ttname"><a href="group__types.html#ga38500bd43c27c05b7f8a566cefe94656">scope_name</a></div><div class="ttdeci">uint64_t scope_name</div><div class="ttdef"><b>Definition:</b> types.h:40</div></div>
<div class="ttc" id="db_8hpp_html"><div class="ttname"><a href="db_8hpp.html">db.hpp</a></div></div>
<div class="ttc" id="classeosio_1_1datastream_html"><div class="ttname"><a href="classeosio_1_1datastream.html">eosio::datastream</a></div><div class="ttdoc">A data stream for reading and writing data in the form of bytes. </div><div class="ttdef"><b>Definition:</b> datastream.hpp:14</div></div>
<div class="ttc" id="classeosio_1_1singleton_html_a0c3a5437e9b68a71009537a53a9bfbc5"><div class="ttname"><a href="classeosio_1_1singleton.html#a0c3a5437e9b68a71009537a53a9bfbc5">eosio::singleton::exists</a></div><div class="ttdeci">static bool exists(scope_name scope=Code)</div><div class="ttdef"><b>Definition:</b> singleton.hpp:20</div></div>
<div class="ttc" id="namespaceeosio_1_1raw_html_ab1d3be0baf9bbbf0977000e24a3bec92"><div class="ttname"><a href="namespaceeosio_1_1raw.html#ab1d3be0baf9bbbf0977000e24a3bec92">eosio::raw::unpack</a></div><div class="ttdeci">void unpack(Stream &amp;ds, T &amp;t)</div><div class="ttdef"><b>Definition:</b> raw.hpp:27</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
